package org.ilead.iserve.common.email;

import java.io.UnsupportedEncodingException;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;


public class HtmlEmailSender {
   public static void sendMessage(String smtpHost,
                                  String from,String fromName, String password, String to,
                                  String subject, String messageText)
           throws MessagingException,java.io.UnsupportedEncodingException {
 
       // Step 1:  Configure the mail session
       java.util.Properties props = new java.util.Properties();
       props.setProperty("mail.smtp.host", smtpHost);//指定SMTP服务器
       props.put("mail.transport.protocol", "smtp");
       props.put("mail.smtp.auth", "true");
       Session mailSession = Session.getInstance(props, new EmailAuthenticator(from, password));

       // Step 2:  Construct the message
       InternetAddress fromAddress = new InternetAddress(from);
       InternetAddress toAddress = new InternetAddress(to);
 
       MimeMessage testMessage = new MimeMessage(mailSession);
       testMessage.setFrom(fromAddress);
       testMessage.addRecipient(javax.mail.Message.RecipientType.TO, toAddress);
       testMessage.setSentDate(new java.util.Date());
       testMessage.setSubject(MimeUtility.encodeText(subject,"gb2312","B"));
 
       testMessage.setContent(messageText, "text/html;charset=gb2312");
       System.out.println("Message constructed");
 
       // Step 3:  Now send the message
       Transport transport = mailSession.getTransport("smtp");
       transport.connect(smtpHost, fromName, password);
       transport.sendMessage(testMessage, testMessage.getAllRecipients());
       transport.close();

       System.out.println("Message sent!");
   }
   
   public static void sendMessage(String to,
           String subject, String messageText) throws MessagingException,java.io.UnsupportedEncodingException {
//	   sendMessage("smtp.sohu.com", "lovelop@sohu.com", "lovelop", "jilili.03", to, subject, messageText);
//	   sendMessage("smtp.qq.com", "1941693266@qq.com", "lop", "jilili", to, subject, messageText);
//	   sendMessage("smtp.mail.yahoo.com.cn", "jilili03@yahoo.com.cn", "jilili03", "520131", to, subject, messageText);
//	   sendMessage("mail.icinfo.com.cn", "whf@icinfo.com.cn", "peterwu1984", "peterwu1984", to, subject, messageText);
	   sendMessage("smtp.sina.com", "landofpromise@sina.com", "landofpromise", "20110821", to, subject, messageText);
//	   sendMessage("smtp.qq.com", "270210858@q.com", "landofpromise", "jilili0521", to, subject, messageText);
   }
   
   public static void main(String[] args) {
	try {
		sendMessage("270210858@qq.com","jilili,请激活你的LOP网站帐户","<br><font size=\"2\">感谢您注册应许之地的官方网站</font><br><br><font size=\"2\">请点击下面的链接激活您的帐户</font>" +
				"<br><font size=\"2\">感谢您注册LOP网站</font><br><br><font size=\"3\">请点击下面的链接激活您的帐户</font><br>" +
				"<a href=\"http://www.landofpromise.co/verify.action?id=17&code=c87e7b96-38bf-4a5a-a3bf-69aa41a27d8a7470356c-32bf-44e7-97fb-92b087c1056f\">http://www.landofpromise.co/verify.action?id=17&code=c87e7b96-38bf-4a5a-a3bf-69aa41a27d8a7470356c-32bf-44e7-97fb-92b087c1056f</a><br>");
	} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (MessagingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
   }
 
}

